package com.deyuanyun.pic.pipe.controller;

import com.caixu.beanvalidateutil.imlclass.BeanValidateUtil;
import com.caixu.beanvalidateutil.imlclass.VResult;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.tool.StaticParam;
import com.deyuanyun.pic.common.util.MyCollectionUtil;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxResponse;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ReadExcel;
import com.deyuanyun.pic.common.util.poi.WriteExcel;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pipe.controller.publiccontroller.DownLoadController;
import com.deyuanyun.pic.pipe.domain.custom.CustomPicPipePiggingstructure;
import com.deyuanyun.pic.pipe.domain.exceldomain.PiggingStructureVO;
import com.deyuanyun.pic.pipe.service.PipePiggingStructureService;
import com.deyuanyun.pic.pipe.support.other.QueryParamUtil;
import com.deyuanyun.pic.pipe.support.utils.UniformCodeUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2016/1/29.
 * 收发球筒
 */
@Controller
@RequestMapping("/pipe/data/piggingstructure")
public class PipePiggingStructureController extends PipeBaseController {
    static Logger logger=Logger.getLogger(PipePiggingStructureController.class);
    @Autowired
    private PipePiggingStructureService pipePiggingStructureService;

    @RequestMapping("delPiggingstructureRecord.json")
    @ResponseBody
    public void delPiggingstructureRecord(String id){
        Asserts.assertTrue(StringUtils.isNotBlank(id),"删除id不能为空");
        pipePiggingStructureService.delPiggingstructureRecord(id);
        AjaxSupport.sendSuccessText("success","success");
    }

    /**
     * 导出
     * @param response
     * @param pipeLineId
     * @param segmentId
     * @param juliStart
     * @param juliEnd
     * @throws Exception
     */
    @RequestMapping("piggingstructureExport.json")
    @ResponseBody
    public void piggingstructureExport(HttpServletResponse response,
                                       String pipeLineId,
                                       String segmentId,
                                       String juliStart,
                                       String juliEnd)throws Exception{
        Map map=new HashMap();
        map.put("pipeLineId",pipeLineId);
        map.put("segmentId",segmentId);
        QueryParamUtil.queryJuLiMap(juliStart,juliEnd,map);
        List<CustomPicPipePiggingstructure> picPipePiggingstructures=pipePiggingStructureService.queryPiggingstructureList(map);
        List<PiggingStructureVO> piggingStructureVOs=new ArrayList<>();
        for (CustomPicPipePiggingstructure piggingstructure:picPipePiggingstructures){
            piggingStructureVOs.add(piggingstructure.toVO());
        }
        String templateName="out/pigging_structure_template.xls";
        Workbook workbook=ReadExcel.readExcelTemplate(templateName);
        Workbook workbookFinal = WriteExcel.writeExcel(workbook,piggingStructureVOs,1, Lists.<String>newArrayList("priUuid","laocationX","laocationY","laocationZ"));
        DownLoadController.initExcelDownResponse(response, StringUtils.removeEnd(templateName,"Template"));
        DownLoadController.excelOut(response,workbookFinal);
        return;

    }


    /**
     * 查询收发球筒
     * @param pipeLineId
     * @param segmentId
     * @param juliStart
     * @param juliEnd
     */
    @RequestMapping("queryPiggingstructureList.json")
    @ResponseBody
    public void queryPiggingstructureList(String pipeLineId,String segmentId,String rooms,String juliStart,String juliEnd){
        Map map=new HashMap();
        map.put("pipeLineId",pipeLineId);
        map.put("segmentId",segmentId);
        map.put("rooms",rooms);
        QueryParamUtil.queryJuLiMap(juliStart,juliEnd,map);
        PageJsonBean jsonBean=new PageJsonBean();
        List<CustomPicPipePiggingstructure> picPipePiggingstructures=pipePiggingStructureService.queryPiggingstructureList(map);
        jsonBean.setList(picPipePiggingstructures);
        AjaxSupport.sendSuccessText("success",jsonBean);
        return;
    }


    /**
     * 导入收发球筒
     * @param multipartFiles
     * @param response
     * @throws Exception
     */
    @RequestMapping("piggingstructureImport.json")
    @ResponseBody
    public void piggingstructureImport(@RequestParam("file")MultipartFile[] multipartFiles, HttpServletResponse response)throws Exception{
        Asserts.assertTrue(!ObjectUtil.isEmptyAll(multipartFiles),"附件不能为空");
        List<PiggingStructureVO> xx= ReadExcel.readExcle(PiggingStructureVO.class,multipartFiles[0].getInputStream(),4,"21");
        List<PiggingStructureVO> finalInfo= MyCollectionUtil.removeLastEmpty(xx);
        VResult b= BeanValidateUtil.vali2(finalInfo,4+"");

        if (b.isRelult()){
            finalInfo=UniformCodeUtils.sortedByLine(finalInfo,"pipeLineId","","code","收发球装置");
            pipePiggingStructureService.insertPiggingstructureList(finalInfo);
            AjaxResponse.sendText(response, StaticParam.UPLOAD_FILE_SUCCESS);
        }else {
            String[] titleArr= ReadExcel.getTitle(multipartFiles[0],"in/pigging_structure_t.xls");
            String m=b.getAllString(titleArr);
            response.setStatus(503);
            AjaxResponse.sendText(response,StaticParam.getUploadFileFailMessage(m));
        }
        return;
    }
}
